////页面控制
//基本跳转
function jumpToPage(pageJumpsInfo) {
    var html = $("html").html();
    cordova.exec(function () {
    }, function () {
    }, 'pagesService', 'jumpNewPage', [pageJumpsInfo, html]);
}
function backToForwardPage() {
    cordova.exec(function () {
    }, function () {
    }, 'pagesService', 'goback', []);
}
// 杀掉launch页
function killLaunch(photoKey, callback) {
    cordova.exec(callback, callback, 'pagesService', 'killLaunch', [photoKey]);
}
//显示页面
function showMeRightNow() {
    cordova.exec(function () {
    }, function () {
    }, 'pagesService', 'show', []);
}
//执行本地SQL
function execSQL(sql, params, callback) {
    cordova.exec(function (data) {
        try {
            data = JSON.parse(data)
        } catch (e) {
        }
        callback(data)
    }, function () {
    }, 'DBHelper', 'findDB', [sql, params]);
}
// 提交
function submitToAjaxWorker(params, callback) {
    cordova.exec(callback, callback, 'AjaxWorker', 'requestToAjax', [params]);
}
function saveToSharedPreferences(type, key, value, callback) {
    cordova.exec(callback, callback, 'DBHelper', 'saveToSharedPreferences', [type, key, value]);
}
function findSharedPreferences(type, key, callback) {
    cordova.exec(callback, callback, 'DBHelper', 'findSharedPreferences', [type, key]);
}
//退出登录
function logOutUser(callback) {
    cordova.exec(callback, callback, 'UserManager', 'logoutUser', []);
}
//检查更新
function checkUpgrade(callback) {
    cordova.exec(callback, callback, 'UserManager', 'checkUpdate', []);
}
//获取登录用户信息
function getUserInfo(callback) {
    cordova.exec(callback, callback, 'UserManager', 'getUserInfo', []);
}
// 同步服务端数据
function syncData(tables, callback) {
    cordova.exec(callback, callback, 'AjaxWorker', 'loadAndSave', [tables]);
}
//上传指定ajaxwork
function uploadAjaxTask(ajaxIDs, callback) {
    var uploadAjaxTaskCallback = function () {
    };
    if (callback != undefined) {
        uploadAjaxTaskCallback = callback
    }
    cordova.exec(uploadAjaxTaskCallback, uploadAjaxTaskCallback, 'AjaxWorker', 'autoRequestByIDs', [ajaxIDs]);
}
//上传全部ajaxwork
function uploadAllAjaxTask(callback) {
    cordova.exec(callback, callback, 'AjaxWorker', 'autoRequest', []);
}
// 获取图片上传进度
function getPhotosProgress(callback) {
    cordova.exec(callback, callback, 'AjaxWorker', 'getAllProgress', []);
}
// 计算未读数
function messageCountController(type, count, callback) {
    cordova.exec(callback, callback, 'AjaxWorker', 'messageCountController', [type, count]);
}
//图片压缩
function graphicsOptimiser(options, callback) {
    cordova.exec(callback, callback, 'FileHelper', 'getThumbnailImgFile', [options.width, options.height, options.imgURL]);
}
// 下载小图
function downloadPhoto(photoKey, callback) {
    cordova.exec(callback, callback, 'FileHelper', 'downloadFile', [photoKey]);
}
//打开图片浏览器(点击看大图)
function openPhotoBrowse(photoKeys, currIndex, callback) {
    cordova.exec(callback, callback, 'FileHelper', 'PhotoBrowse', [photoKeys, currIndex]);
}

function beginAsyncAjax(url,data,type,callback, options) {
    cordova.exec(callback, callback, 'AjaxWorker', 'requestURL', [url,data,type,options]);
}

function setAppMode(value, callback) {
    if(value){
        value = "ON";
    }else{
        value = "OFF";
    }
    saveToSharedPreferences("private", "appMode", value, callback);
}

function getAppMode(callback) {
    findSharedPreferences("private", "appMode", callback);
}



/*function getPhotos(callback) {
    openCamera(function (data) {
        var originalImgUrl = data;
        var options = {
            imgURL: originalImgUrl,
            width: 50,
            height: 50
        };
        graphicsOptimiser(options, function (data) {
            var time = new Date().getTime();
            var thumbnailImgUrl = data;
            var imgName = thumbnailImgUrl.split("/")[thumbnailImgUrl.split("/").length - 1];
            var imgNameArr = imgName.split(".");
            var imgNameArrLength = imgName.split(".").length;
            var extention = "."+imgNameArr[imgNameArrLength-1];
            var photoItem = {
                "originalFileKey": "original" + time + extention,
                "originalFilePath": originalImgUrl,
                "thumbnailFileKey": "thumbnail" + time + extention,
                "thumbnailFilePath": thumbnailImgUrl
            };
            callback(photoItem)
        })
    })
}*/
/* 图片 上传 格式 不同
function myFileChooser(callback) {
    var photos = [];

    function graphicsOptimiserCallBack(dataArr) {
        // 当拍照返回图片数组为空时，不执行任何操作。
        if(dataArr.length == 0 && dataArr == "[]") {
            return;
        }
        dataArr = JSON.parse(dataArr);
        var i = 0;
        var graphicsOptimiserPhotoItem = function () {
            var originalImgUrl = dataArr[i];
            var options = {
                imgURL: originalImgUrl,
                width: 50,
                height: 50
            };
            graphicsOptimiser(options, function (data) {
                var time = new Date().getTime();
                var thumbnailImgUrl = data;
                var imgName = thumbnailImgUrl.split("/")[thumbnailImgUrl.split("/").length - 1];
                var imgNameArr = imgName.split(".");
                var imgNameArrLength = imgName.split(".").length;
                var extention = "."+imgNameArr[imgNameArrLength-1];
                var photoItem = {
                    "originalFileKey": "original" + time + extention,
                    "originalFilePath": originalImgUrl,
                    "thumbnailFileKey": "thumbnail" + time + extention,
                    "thumbnailFilePath": thumbnailImgUrl
                };
                photos.push(photoItem);
                i++;
                if(i < dataArr.length){
                    graphicsOptimiserPhotoItem();
                } else {
                    callback(photos);
                }
            });
        }
        graphicsOptimiserPhotoItem();
    }

    cordova.exec(graphicsOptimiserCallBack, graphicsOptimiserCallBack, 'MyFileChooser', '', []);
}*/
// 打开actionsheep  判断选择相机还是相册 并将选择的图片压缩
function myFileChooser(callback) {
    var photos = [];

    function graphicsOptimiserCallBack(dataArr) {
        //alert(dataArr);
        // 当拍照返回图片数组为空时，不执行任何操作。
        if(dataArr.length == 0 || dataArr == "[]") {
            return;
        }
        dataArr = JSON.parse(dataArr);
        var i = 0;
        var graphicsOptimiserPhotoItem = function () {
            var originalImgUrl = dataArr[i];
            var options = {
                imgURL: originalImgUrl,
                width: 50,
                height: 50
            };
            graphicsOptimiser(options, function (data) {
                var time = new Date().getTime();
                var thumbnailImgUrl = data;
                var imgName = thumbnailImgUrl.split("/")[thumbnailImgUrl.split("/").length - 1];
                var imgNameArr = imgName.split(".");
                var imgNameArrLength = imgName.split(".").length;
                var extention = "."+imgNameArr[imgNameArrLength-1];
                var photoItem = {
                    "originalFileKey": + time + extention,
                    "originalFilePath": originalImgUrl,
                    "thumbnailFileKey": "96x96_" + time + extention,
                    "thumbnailFilePath": thumbnailImgUrl
                };
                photos.push(photoItem);
                i++;
                if(i < dataArr.length){
                    graphicsOptimiserPhotoItem();
                } else {
                    callback(photos);
                }
            });
        }
        graphicsOptimiserPhotoItem();
    }

    cordova.exec(graphicsOptimiserCallBack, graphicsOptimiserCallBack, 'MyFileChooser', '', []);
}
